package com.xzcedu.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 徐州城
 * @version 1.0
 * Matcher 类常用方法
 */
public class MatcherMethod {
    public static void main(String[] args) {
        String content = "hello jdk edu jack mysql hello hspedu hspedu";
        String regStr = "hello";

        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);

        while (matcher.find()){
            System.out.println("=============");
            System.out.println("找到: " + matcher.group(0));
            System.out.println(matcher.start());
            System.out.println(matcher.end());
        }

        //整体匹配方法，常用于，去校验某个字符串是否满足某个规则
        System.out.println("整体匹配=" + matcher.matches());

        //完成如果content有 hspedu 替换成 韩顺平教育
        regStr = "hspedu";
        pattern = Pattern.compile(regStr);
        matcher = pattern.matcher(content);
        //注意: 返回的字符串才是替换后的字符串，原来的content不变化
        //String newContent = matcher.replaceAll("韩顺平教育");
        content = matcher.replaceAll("韩顺平教育");
        //System.out.println("newContent" + newContent);
        System.out.println("content" + content);
    }
}
